package com.mindata.blockchain.socket.handler.server;

import com.mindata.blockchain.ApplicationContextProvider;
import com.mindata.blockchain.core.event.AddInstructionEvent;
import com.mindata.blockchain.socket.base.AbstractBlockHandler;
import com.mindata.blockchain.socket.body.InstructionBody;
import com.mindata.blockchain.socket.packet.BlockPacket;
import lombok.extern.slf4j.Slf4j;
import org.tio.core.ChannelContext;

/**
 * 新增指令请求操作
 * <p>
 * User: jasperxgwang
 * Date: 2018-7-30 11:29
 */
@Slf4j
public class AddInstructionRequestHandler extends AbstractBlockHandler<InstructionBody> {

    @Override
    public Class<InstructionBody> bodyClass() {
        return InstructionBody.class;
    }

    /**
     * 实际的handler处理
     *
     * @param packet         packet
     * @param bsBody         解析后的对象
     * @param channelContext channelContext
     * @return 用不上
     * @throws Exception Exception
     */
    @Override
    public Object handler(BlockPacket packet, InstructionBody bsBody, ChannelContext channelContext) {
        log.info("收到来自于<" + bsBody.getAppId() + "><增加指令>消息，指令内容为[" + bsBody.getInstructions() + "]");
        ApplicationContextProvider.publishEvent(new AddInstructionEvent(bsBody.getInstructions()));
        return null;
    }
}
